<?php
class Administrator_administration_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }	
	
	function mainmenu_management_add()
	{
		$main_name = $this->input->post('main_name');
		$main_icon = $this->input->post('main_icon');
		
		$data = array(
			'main_name' => $main_name,
			'main_icon' => $main_icon,
			'main_page' => strtolower(preg_replace("/[^a-zA-z0-9]/","_",$main_name))
		);
		$this->db->insert('mainmenu',$data);	
	}
	
	function mainmenu_management_edit($id)
	{
		$main_name = $this->input->post('main_name');
		$main_icon = $this->input->post('main_icon');
		
		$data = array(
			'main_name' => $main_name,
			'main_icon' => $main_icon,
			'main_page' => strtolower(preg_replace("/[^a-zA-z0-9]/","_",$main_name))			
		);
		$this->db->update('mainmenu',$data,array('mainmenu_id' => $id));
	}	
	
	function mainmenu_management_delete()
	{
		$delete_menu = $this->input->post('delete_menu');
		foreach($delete_menu as $value):
			//Check rebuilt Session
			$is_exist = $this->db->get_where('group_permission',array('group_id' => $this->session->userdata('group_id'),'mainmenu_id' => $value),1)->row();
			if(!empty($is_exist)) $rebuilt_session = 1;
					
			$tables = array('mainmenu','submenu','extramenu','group_permission');
			$this->db->where('mainmenu_id', $value);
			$this->db->delete($tables);		
		endforeach;
		//rebuit Session
		if(!empty($rebuilt_session)):
			$generate_menu = $this->generate_menu($this->session->userdata('group_id'));
			$this->session->set_userdata(array('generate_menu' => $generate_menu));			
		endif;		
	}
	
	function mainmenu_management_update()
	{
		$sort_id = $this->input->post('mainsort_id');
		foreach($sort_id as $key => $value):
			$this->db->update('mainmenu',array('mainsort_id' => $value),array('mainmenu_id' => $key));
		endforeach;
	}	
	
	function submenu_management_add()
	{
		$mainmenu_id = $this->input->post('mainmenu_id');
		$sub_name = $this->input->post('sub_name');
		$staus = $this->input->post('status');
		
		$data = array(
			'mainmenu_id' => $mainmenu_id,
			'sub_name' => $sub_name,
			'sub_page' => strtolower(preg_replace("/[^a-zA-z0-9]/","_",$sub_name)),
			'status' => $staus		
		);
		$this->db->insert('submenu',$data);
	}
	
	function submenu_management_edit($id)
	{
		$mainmenu_id = $this->input->post('mainmenu_id');
		$sub_name = $this->input->post('sub_name');
		$staus = $this->input->post('status');				
		
		$data = array(
			'mainmenu_id' => $mainmenu_id,
			'sub_name' => $sub_name,
			'sub_page' => strtolower(preg_replace("/[^a-zA-z0-9]/","_",$sub_name)),
			'status' => $staus			
		);
		$this->db->update('submenu',$data,array('submenu_id' => $id));
	}	
	
	function submenu_management_delete()
	{
		$delete_menu = $this->input->post('delete_menu');
		foreach($delete_menu as $value):
			//Check rebuilt Session
			$is_exist = $this->db->get_where('group_permission',array('group_id' => $this->session->userdata('group_id'),'submenu_id' => $value),1)->row();
			if(!empty($is_exist)) $rebuilt_session = 1;
			
			$tables = array('submenu','extramenu','group_permission');
			$this->db->where('submenu_id', $value);
			$this->db->delete($tables);		
		endforeach;
		//rebuit Session		
		if(!empty($rebuilt_session)):
			$generate_menu = $this->generate_menu($this->session->userdata('group_id'));
			$this->session->set_userdata(array('generate_menu' => $generate_menu));			
		endif;
	}
	
	function submenu_management_update()
	{
		$sort_id = $this->input->post('subsort_id');
		foreach($sort_id as $key => $value):
			$this->db->update('submenu',array('subsort_id' => $value),array('submenu_id' => $key));
		endforeach;
	}
	
	function extramenu_management_add()
	{
		$mainmenu_id = $this->input->post('mainmenu_id');
		$submenu_id = $this->input->post('submenu_id');		
		$extra_name = $this->input->post('extra_name');
		$staus = $this->input->post('status');
		
		$data = array(
			'mainmenu_id' => $mainmenu_id,
			'submenu_id' => $submenu_id,
			'extra_name' => $extra_name,
			'extra_page' => strtolower(preg_replace("/[^a-zA-z0-9]/","_",$extra_name)),
			'status' => $staus		
		);
		$this->db->insert('extramenu',$data);
	}
	
	function extramenu_management_edit($id)
	{
		$mainmenu_id = $this->input->post('mainmenu_id');
		$submenu_id = $this->input->post('submenu_id');
		$extra_name = $this->input->post('extra_name');
		$staus = $this->input->post('status');				
		
		$data = array(
			'mainmenu_id' => $mainmenu_id,
			'submenu_id' => $submenu_id,
			'extra_name' => $extra_name,
			'extra_page' => strtolower(preg_replace("/[^a-zA-z0-9]/","_",$extra_name)),
			'status' => $staus			
		);
		$this->db->update('extramenu',$data,array('extramenu_id' => $id));
	}	
	
	function extramenu_management_delete()
	{
		$delete_menu = $this->input->post('delete_menu');
		foreach($delete_menu as $value):	
			$tables = array('extramenu');
			$this->db->where('extramenu_id', $value);
			$this->db->delete($tables);		
		endforeach;
	}
	
	function extramenu_management_update()
	{
		$sort_id = $this->input->post('extrasort_id');
		foreach($sort_id as $key => $value):
			$this->db->update('extramenu',array('extrasort_id' => $value),array('extramenu_id' => $key));
		endforeach;
	}	
	
	function group_management_add()
	{
		$group_name = $this->input->post('group_name');
		$mainmenu_id = $this->input->post('mainmenu_id');
		$submenu_id = $this->input->post('submenu_id');
		$datetime = date('Y-m-d H:i:s');
		
		//Insert group
		$data = array(
			'group_name' => $group_name,
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('group',$data);
		$insert_id = $this->db->insert_id();
		
		//Insert group_permission
		$multidata = array();
		if(!empty($mainmenu_id)) foreach($mainmenu_id as $value):
			foreach($submenu_id[$value] as $key => $item):
				$multidata[] = array(
					'group_id' => $insert_id,
					'mainmenu_id' => $value,
					'submenu_id' => $item
				);				
			endforeach;	
		endforeach;
		$this->db->insert_batch('group_permission',$multidata);		
	}
	
	function group_management_edit($id)
	{
		$group_name = $this->input->post('group_name');
		$mainmenu_id = $this->input->post('mainmenu_id');
		$submenu_id = $this->input->post('submenu_id');
		$datetime = date('Y-m-d H:i:s');
		
		//Update group
		$data = array(
			'group_name' => $group_name,
			'update_date' => $datetime
		);
		$this->db->update('group',$data,array('group_id' => $id));
		
		//Insert group_permission
		$this->db->delete('group_permission',array('group_id' => $id));
		$multidata = array();
	
		if(!empty($mainmenu_id)) foreach($mainmenu_id as $value):
			if(!empty($submenu_id[$value])):
				foreach($submenu_id[$value] as $key => $item):
					$multidata[] = array(
						'group_id' => $id,
						'mainmenu_id' => $value,
						'submenu_id' => $item
					);				
				endforeach;	
			endif;
		endforeach;
		if(!empty($multidata)) $this->db->insert_batch('group_permission',$multidata);
		
		//re-built Session
		if($this->session->userdata('group_id') == $id):
			$generate_menu = $this->generate_menu($this->session->userdata('group_id'));
			$this->session->set_userdata(array('generate_menu' => $generate_menu));	
		endif;
	}	
	
	function group_management_delete()
	{
		$delete_menu = $this->input->post('delete_menu');
		foreach($delete_menu as $value):
			$tables = array('group','group_permission');
			$this->db->where('group_id', $value);
			$this->db->delete($tables);		
		endforeach;
	}
	
	public function generate_menu($group_id)
	{
		//High Algolythm
		$dataPermission = $this->db->join('mainmenu as j','j.mainmenu_id=t.mainmenu_id','left')->join('submenu as k','k.submenu_id=t.submenu_id','left')->order_by('j.mainsort_id ASC,k.subsort_id ASC')->get_where('group_permission as t',array('t.group_id' => $group_id))->result();
		$generate_menu = array();
		$array_id = array();	
		foreach($dataPermission as $key => $value):
			if(!in_array($value->mainmenu_id,$array_id)):
				$generate_menu[$value->mainmenu_id] = array();
				$generate_menu[$value->mainmenu_id]['main_name'] = $value->main_name;			
				$generate_menu[$value->mainmenu_id]['main_icon'] = $value->main_icon;
				$generate_menu[$value->mainmenu_id]['main_page'] = $value->main_page;			
				$generate_menu[$value->mainmenu_id]['submenu'] = array();
				//Add Menu in array
				$array_id[] = $value->mainmenu_id;				
			endif;
			$generate_menu[$value->mainmenu_id]['submenu'][$value->submenu_id] = array(
				'sub_name' => $value->sub_name,
				'sub_page' => $value->sub_page
			);			
		endforeach;
		return $generate_menu;	
	}
}

?>